/**
 * 完结审批
 * Created by xc on 2015/6/17.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('CostCheckCtrl', CostCheckCtrl);
    CostCheckCtrl.$inject = ['MHService'];
    function CostCheckCtrl(MHService) {
      var costCheck = this,
        currentProcedures = MHService.globalData(false, {key: 'currentProcedures'}),//当前流程
        applyid = currentProcedures.applyid,//applyid
        gardenid = MHService.globalData(false, {key: 'projectId'}),//项目id
        processid = MHService.globalData(false, {key: 'processId'}),//工序id
        procedureid = MHService.globalData(false, {key: 'procedureid'}),//流程id;
        query_path = MHService.URL.query_cost + '?gardenid=' + gardenid + '&proid=' + procedureid + '&processid=' + processid;//完结详情url
      //首先获取完结详情
      costCheck.currentname = currentProcedures.name;//当前流程名称

      MHService.getReq({url: query_path}, function (result) {
        costCheck.startTime = result.gfd.starttime.time;
        costCheck.endTime = result.gfd.endtime.time;
        //处理数据
        angular.forEach(result.data, function (obj) {
          if (applyid === obj.id) {
            costCheck.currentCost = obj;
            costCheck.images = obj.gpas;
          }
        });
      });
      function submit(data) {
        MHService.postReq({url: MHService.URL.cost_check, _req: data}, function (result) {
          MHService.Toast('审批成功');
          history.go(-2);
        });
      }

      /**
       * 同意
       */
      costCheck.agree = function () {
        var _req = {
          applyid: applyid,
          content: costCheck.description,
          sbn: '同意'
        };
        submit(_req);
      };
      /**
       * 拒绝
       */
      costCheck.denied = function () {
        var _req = {
          applyid: applyid,
          content: costCheck.description,
          sbn: '驳回'
        };
        submit(_req);
      };
    }
  };
});
